﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using Eniux.Windows.ThreeDEngine.Helper;
using System.Windows;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    /// 
    /// </summary>
    public class PieSliceVisual3D : MeshElement3D
    {
        #region 注册依赖属性
        #region Center（中心点）
        /// <summary>
        /// 获取或设置中心点
        /// </summary>
        public Point3D Center
        {
            get { return (Point3D)GetValue(CenterProperty); }
            set {SetValue(CenterProperty, value); }
        }
        /// <summary>
        /// 中心点属性
        /// </summary>
        public static readonly DependencyProperty CenterProperty = DependencyProperty.Register(
            "Center", typeof(Point3D), typeof(PieSliceVisual3D), new UIPropertyMetadata(new Point3D())); 
        #endregion

        #region StartAngle（开始角度）
        /// <summary>
        /// 获取或设置开始角度
        /// </summary>
        public double StartAngle
        {
            get { return (double)GetValue(StartAngleProperty); }
            set {SetValue(StartAngleProperty, value); }
        }
        /// <summary>
        /// 开始角度属性
        /// </summary>
        public static readonly DependencyProperty StartAngleProperty = DependencyProperty.Register(
            "StartAngle", typeof(double), typeof(PieSliceVisual3D), new UIPropertyMetadata(0.0));
        #endregion

        #region EndAngle（结束角度）
        /// <summary>
        /// 获取或设置结束角度
        /// </summary>
        public double EndAngle
        {
            get { return (double)GetValue(EndAngleProperty); }
            set {SetValue(EndAngleProperty, value); }
        }
        /// <summary>
        /// 结束角度属性
        /// </summary>
        public static readonly DependencyProperty EndAngleProperty = DependencyProperty.Register(
            "EndAngle", typeof(double), typeof(PieSliceVisual3D), new UIPropertyMetadata(90.0)); 
        #endregion

        #region InnerRadius（内径）
        /// <summary>
        /// 获取或设置内径
        /// </summary>
        public double InnerRadius
        {
            get { return (double)GetValue(InnerRadiusProperty); }
            set {SetValue(InnerRadiusProperty, value); }
        }
        /// <summary>
        /// 内径属性
        /// </summary>
        public static readonly DependencyProperty InnerRadiusProperty = DependencyProperty.Register(
            "InnerRadius", typeof(double), typeof(PieSliceVisual3D), new UIPropertyMetadata(0.5)); 
        #endregion

        #region OuterRadius（外径）
        /// <summary>
        /// 获取或设置外径
        /// </summary>
        public double OuterRadius
        {
            get { return (double)GetValue(OuterRadiusProperty); }
            set {SetValue(OuterRadiusProperty, value); }
        }
        /// <summary>
        /// 外径属性
        /// </summary>
        public static readonly DependencyProperty OuterRadiusProperty = DependencyProperty.Register(
            "OuterRadius", typeof(double), typeof(PieSliceVisual3D), new UIPropertyMetadata(1.0)); 
        #endregion

        #region Normal（法线）
        /// <summary>
        /// 获取或设置法线
        /// </summary>
        public Vector3D Normal
        {
            get { return (Vector3D)GetValue(NormalProperty); }
            set {SetValue(NormalProperty, value); }
        }
        /// <summary>
        /// 法线属性
        /// </summary>
        public static readonly DependencyProperty NormalProperty = DependencyProperty.Register(
            "Normal", typeof(Vector3D), typeof(PieSliceVisual3D), new UIPropertyMetadata(new Vector3D(0, 0, 1)));
        #endregion

        #region ThetaDiv
        /// <summary>
        /// 获取或设置ThetaDiv
        /// </summary>
        public int ThetaDiv
        {
            get { return (int)GetValue(ThetaDivProperty); }
            set {SetValue(ThetaDivProperty, value); }
        }
        /// <summary>
        /// ThetaDiv属性
        /// </summary>
        public static readonly DependencyProperty ThetaDivProperty = DependencyProperty.Register(
            "ThetaDiv", typeof(int), typeof(PieSliceVisual3D), new UIPropertyMetadata(20)); 
        #endregion

        #region UpVector（向上向量）
        /// <summary>
        /// 获取或设置向上向量
        /// </summary>
        public Vector3D UpVector
        {
            get { return (Vector3D)GetValue(UpVectorProperty); }
            set {SetValue(UpVectorProperty, value); }
        }
        /// <summary>
        /// 向上向量属性
        /// </summary>
        public static readonly DependencyProperty UpVectorProperty = DependencyProperty.Register(
            "UpVector", typeof(Vector3D), typeof(PieSliceVisual3D), new UIPropertyMetadata(new Vector3D(0, 1, 0))); 
        #endregion

        #endregion

        #region 方法

        /// <summary>
        /// 生成几何模型
        /// </summary>
        /// <returns></returns>
        protected override MeshGeometry3D Tessellate()
        {
            var pts = new Point3DCollection();
            var right = Vector3D.CrossProduct(UpVector,Normal);
            for (int i = 0; i <ThetaDiv; i++)
            {
                double angle =StartAngle + (EndAngle -StartAngle) * i / (ThetaDiv - 1);
                double angleRad = angle / 180 * Math.PI;
                Vector3D dir = right * Math.Cos(angleRad) +UpVector * Math.Sin(angleRad);
                pts.Add(Center + dir *InnerRadius);
                pts.Add(Center + dir *OuterRadius);
            }

            var b = new MeshBuilder();
            b.AddTriangleStrip(pts, null, null);
            return b.ToMesh();
        }

        #endregion
    }
}
